﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * ZIP Archive Extra Data Record Class
 * 		Represents and manages the archive extra data record that may follow the
 * file entries and preceeds the central directory record.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;

namespace Adaptive.Foundation.IO.Zip
{
    /// <summary>
    /// Represents and manages the archive extra data record that may follow the
    /// file entries and preceeds the central directory record.
    /// </summary>
    /// <remarks>
    /// This record exists in support of the Central Directory Encryption Feature 
    /// implemented as part of the Strong Encryption Specification.
    /// </remarks>
    public sealed class ZipArchiveExtraDataRecord : AiObjectBase, IStorable
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Data entry signature.
        /// </summary>
        private byte[] _signature = new byte[] { };
        /// <summary>
        /// Size of the data.
        /// </summary>
        private int _extraFieldLength;
        /// <summary>
        /// Extra data content.
        /// </summary>
        private byte[] _extraFieldData;
        #endregion

        /*--------------------------------------------------------------------------------
	     * Constructor / Dispose / Destructor Methods
	     *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the <see cref="ZipArchiveExtraDataRecord" /> class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ZipArchiveExtraDataRecord()
            : base()
        {
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ZipArchiveExtraDataRecord" /> class.
        /// </summary>
        /// <param name="extraDataContent">
        /// A byte array containing the extra data content.
        /// </param>
        public ZipArchiveExtraDataRecord(byte[] extraDataContent)
            : base()
        {
            _extraFieldLength = extraDataContent.Length;
            _extraFieldData = new byte[_extraFieldLength];
            Array.Copy(extraDataContent, _extraFieldData, _extraFieldLength);
        }
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><b>true</b> to release both managed and unmanaged resources;
        /// <b>false</b> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                if (_extraFieldData != null)
                    _extraFieldData = null;
            }

            _extraFieldData = null;
            _signature = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
	     * Public Properties
	     *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// gets the size of the data.
        /// </summary>
        /// <value>
        /// The size of the exta data content, in bytes.
        /// </value>
        public int ExtraFieldLength
        {
            get
            {
                return _extraFieldLength;
            }
        }
        /// <summary>
        /// gets or sets the extra data content.
        /// </summary>
        /// <value>
        /// A byte array containing the extra data content.
        /// </value>
        public byte[] ExtraFieldData
        {
            get
            {
                return _extraFieldData;
            }
            set
        {
            if (value != null)
            {
                _extraFieldLength = value.Length;
                _extraFieldData = new byte[_extraFieldLength];
				Array.Copy(value, _extraFieldData, _extraFieldLength);
            }
            else
            {
                _extraFieldLength = 0;
                _extraFieldData = new byte[0];
            }
        }
        }
        #endregion

        /*--------------------------------------------------------------------------------
	     * Public Methods / Functions
	     *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Loads the content for the instance from the specified stream.
        /// </summary>
        /// <param name="reader">An open <see cref="BinaryReader" /> used to read data from the
        /// underlying stream.</param>
        /// <returns>
        /// <b>true</b> if the load operation is successful; otherwise, <b>false</b>.
        /// </returns>
        public bool Load(BinaryReader reader)
        {
            bool returnValue = false;               //Return value.

            try
            {
                _signature = reader.ReadBytes(4);
                _extraFieldLength = reader.ReadInt32();
                _extraFieldData = reader.ReadBytes(_extraFieldLength);
                returnValue = true;
            }
            catch
            {
            }

            return returnValue;
        }
        /// <summary>
        /// Saves the content for the instance to the specified stream.
        /// </summary>
        /// <param name="writer">An open <see cref="BinaryWriter" /> used to write data to the
        /// underlying stream.</param>
        /// <returns>
        /// <b>true</b> if the save operation is successful; otherwise, <b>false</b>.
        /// </returns>
        public bool Save(BinaryWriter writer)
        {
            bool returnValue = false;

            try
            {
                writer.Write(_signature);

                if (_extraFieldLength > 0)
                {
                    writer.Write(_extraFieldLength);
                    writer.Write(_extraFieldData);
                }
                else
                    writer.Write(0);

                writer.Flush();
                returnValue = true;
            }
            catch
            {
            }

            return returnValue;
        }
        #endregion
    }
}